package com.wu.ssm.spring.aop;

import java.lang.reflect.Method;

/**
 * Created by wzf on 2018/11/25/025.
 */
public abstract class AspectProxy implements Proxy {

    private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(Proxy.class);

    @Override
    public Object doProxy(ProxyChain proxyChain) throws Throwable {

        Object result = null;

        Class<?> clazz = proxyChain.getTargetClazz();
        Method method = proxyChain.getTargetMethod();
        Object [] params = proxyChain.getMethodParams();
        begin();
        try {
            if(intecept(clazz,method,params)){
                before(clazz,method,params);
                result = proxyChain.doProxyChain();
                after(clazz,method,params);
            }else {
                // 再调用链上的方法循环;
                result = proxyChain.doProxyChain();
            }
        } catch (Throwable throwable) {
            LOGGER.error("代理失败");
            error();
            throwable.printStackTrace();
        } finally {
            end();
        }
        return result;
    }


    public void begin() {
    }
    public void before(Class<?> clazz, Method method, Object[] params) {
    }
    public void after(Class<?> clazz, Method method, Object[] params) {
    }
    public void error() {
    }
    public void end() {
    }
    public boolean intecept(Class<?> clazz, Method method, Object[] params) {
        return true;
    }
}
